<?php
namespace Home\Controller;

use Think\Controller;

class MeetController extends Controller
{
	public $output;
	public $tablename = 'meeting_scheduled';
    public function __construct()
    {
    	parent::__construct();
    	$rooms_arr 	=	C('rooms_arr');
    	$time_arr	=	C('time_arr');
    	$room_type_arr	=	C('room_type_arr');
   
		foreach ( $rooms_arr as $room ) {
			foreach ($room_type_arr as $room_type => &$value) {
				if ( $room['type'] == $room_type ) {
					$value ++;
				}
			}
		}
		$time_morning	= 0;
		$time_midday	= 0;
		$time_afterning = 0;
		$time_night		= 0;
		foreach( $time_arr as $tmp_time ) {
			switch( $tmp_time['type'] ){
				case 'morning' :
					$time_morning ++;
					break;
				case 'midday' :
					$time_midday ++;
					break;
				case 'afterning' :
					$time_afterning ++;
					break;
				case 'night' :
					$time_night ++;
					break;
			}
		}

		$this->output['room_type_arr']	= $room_type_arr;
		$this->output['time_morning']		= C('time_morning');
		$this->output['time_midday']		= C('time_midday');
		$this->output['time_afterning']	= C('time_afterning');
		$this->output['time_night']		= C('time_night');

		$this->output['rooms_arr']		= C('rooms_arr');
		$this->output['time_arr']			= $time_arr;
		$this->output['time_pre']			= C('time_pre');
    }

    /**
     * [index description]
     * @return [type] [description]
     */
    public function index(){
    	$action = I('request.act');
		$current_day = I('request.day');
		if ( $action ) {
			switch($action){
				case 'add' :
					$name = I('request.name');
					$room = I('request.room');
					$start = I('request.start');
					$end = I('request.end');
					$mobile = I('request.mobile');
					$remark = I('request.remark');

					setcookie("meeting_name", $name, time()+60*60*24*30 );		//设置为30天有效期

					if ( $start == $end ) {
						echo 'same_err';
						exit;
					}
					
					if ( $start ) $start .= ':00';
					if ( $end ) $end .= ':00';

					$room_date = substr( $start, 0, 10 );

					$sql_ck = "select id from ".$this->tablename." where room_no=$room and ( ('$start'>=start_time and '$end'<=end_time) or ('$start'<=start_time and '$end'>start_time) or ('$start'<end_time and '$end'>=end_time) or ('$start'<=start_time and '$end'>=end_time) )";

					$rs = M('')->execute($sql_ck);

					$save['name'] = $name;
					$save['room_no'] = $room;
					$save['room_date'] = $room_date;
					$save['start_time'] = $start;
					$save['end_time'] = $end;
					$save['mobile'] = $mobile;
					$save['remark'] = $remark;
					$save['display'] = 'T';
					M($this->tablename)->add($save);
					echo '添加成功';
					exit;
					break;
				case 'show':
					break;
			}
		}

		$timestamp = time();
		$this->output['today'] = date("Y-m-d", $timestamp);
		$this->output['timestamp'] = $timestamp;
		$this->output['future_days'] = C('future_days');

		if ( empty($current_day) ) $current_day = $this->output['today'];
		$this->output['current_day'] = $current_day;

		$smap['room_date'] = $current_day;
		$smap['display'] = 'T';

		$result = M($this->tablename)->where($smap)->select();
		
		foreach ($result as $key => $row) {
			$room_no = $row['room_no'];
			$lists[$room_no][] = $row;
		}

		$this->output['hour_arr']		= C('hour_arr');
		$this->output['minute_arr']	= C('minute_arr');

		$this->assign('lists',$lists);
		$this->assign('output',$this->output);
		$this->display('index');

    }

    public function lists(){
    	$action = I('request.act');
		if ( $action ) {
			switch($action){
				case 'del' :
					$id = I('request.id');
					if ( !$id ) {
						echo 'id_err';
						exit;
					}

					$flag_del = false;
					$r_state  = false;

					if ( $is_admin ) {		//白名单中的管理员可以删除任何栏目

						$flag_del = true;

					} else if ( $meeting_name ){		//可以删除自己添加的栏目
						$item_name = M($this->tablename)->where(['id'=>$id])->getField('name');
						if ( $meeting_name == $item_name ) $flag_del = true;
					}

					if ( $flag_del ) {
						$r_state = M($this->tablename)->where(['id'=>$id])->save(['display'=>'F']);
					}

					echo $r_state ? 'succ' : 'fail';
					exit;
					break;
		
			}
		}

		$page_size = 20;

		$total_num = M($this->tablename)->where(['display'=>'T'])->count();

		
		$page = I('request.Page',1);
		
		$result = M($this->tablename)->where(['display'=>'T'])->page($page,$page_size)->select();

		$rooms_arr 	=	C('rooms_arr');
		foreach ($result as $key => &$row) {
			$id = $row['id'];
			$room_no = $row['room_no'];
			$row['room_name'] = $rooms_arr[$room_no]['name'];
		}

		$this->output['lists'] = $result;
		$this->output['total_num'] = $total_num;
		$this->output['page_size'] = $page_size;
		$this->output['page'] = $page;

		$this->assign('output',$this->output);
		$this->display('list');

    }


}